package com.wengu.sort;

import org.junit.jupiter.api.Test;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author whl
 * @Description 排序算法
 * @date 2021/7/2 13:29
 * @Version 1.0
 */
public class BuubleSortDemo {

    @Test
    public void test() {
        int[] arr = {10, 5, 8, 2, 7, -1};
        // 排序前数组
        System.out.println("=========排序前=========");
        print(arr);
        bubbleSort(arr);
        System.out.println("=========排序后=========");
        print(arr);
    }

    @Test
    public void test1() {
        int[] arr = new int[80000];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int) (Math.random() * 8000000);
        }
        long start = System.currentTimeMillis();
        bubbleSort(arr);
        long end = System.currentTimeMillis();
        System.out.println("冒泡排序使用时间为：" + (end - start));
    }

    @Test
    public void test2() {
        Map<String, String> map = new HashMap<>();
        System.out.println(map.put("k1", "v1"));
        System.out.println(map.put("k1", "v2"));
        System.out.println(map.put("k1", "v3"));
    }



    /**
     * 冒泡排序算法
     *
     * @param arr
     */
    public void bubbleSort(int[] arr) {
        int temp;
        boolean flag = true;
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    flag = false;
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
            if (flag) {
                break;
            } else {
                flag = true;
            }
        }
    }

    public void print(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

}
